package com.koron.css2.meterService;


import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.util.DBSourceUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

/**
 * 水表信息
* @author 作者
* @version 创建时间：2018年7月19日 上午11:44:04
* 类说明
*/
@Service
public class MeterService {

	/**
	 * 根据水表编号获取水表信息
	 * @param sessionFactory
	 * @param meterNo
	 * @return
	 * @throws Exception
	 */
	@TaskAnnotation("getMsInfoByNo")
	public MsInfoDto getMsInfoByNo(SessionFactory sessionFactory, String meterNo,String companyNo) throws Exception {
		MsInfoMapper msMapper = sessionFactory.getMapper(MsInfoMapper.class, DBSourceUtils.getDbEnv(companyNo));
		MsInfoDto msInfo = msMapper.getMsInfoByNo(meterNo);// 获取水表信息
		// 获取口径
		if (msInfo != null) {
			if (StringUtils.isNotBlank(msInfo.getMeterForm())) {
				msInfo.setMeterFormName(Tools.getDicNameByCodeAndValue(sessionFactory, "MMF", msInfo.getMeterForm()));
			}
			// 获取型号
/*			if (!StringUtils.isBlank(msInfo.getMeterModel())) {
				MsInfoQueryBean query=new MsInfoQueryBean();
				query.setMeterId(msInfo.getMeterModel());
				MeterModelMapper modelMapper = sessionFactory.getMapper(MeterModelMapper.class);
				List<MeterModelBean> models=modelMapper.queryMeterModelList(query);
				MeterModelBean model=models.get(0);
				msInfo.setMeterModelName(model.getModelName());
				msInfo.setTransmittalModel(Tools.getDicNameByCodeAndValue(sessionFactory, "TPW", model.getTransWay()));
				msInfo.setMeterTypeName(Tools.getDicNameByCodeAndValue(sessionFactory, "MMT", model.getMeterType()));
				msInfo.setMeterBoreName(model.getMeterBoreName());
				msInfo.setMeterFactoryName(model.getFactoryName());	
			}*/
			
			
			
			if (StringUtils.isNotBlank(msInfo.getTransmittalModel())) {
				msInfo.setTransmittalModelName(Tools.getDicNameByCodeAndValue(sessionFactory, "TPW", msInfo.getTransmittalModel()));
			}
			if (StringUtils.isNotBlank(msInfo.getMeterType())) {
				msInfo.setMeterTypeName(Tools.getDicNameByCodeAndValue(sessionFactory, "MMT", msInfo.getMeterType()));
			}
			
			if (StringUtils.isNotBlank(msInfo.getMeterStatus())) {
				msInfo.setMeterStatusName(Tools.getDicNameByCodeAndValue(sessionFactory, "MMS", msInfo.getMeterStatus()));
			}
			if (StringUtils.isNotBlank(msInfo.getCommOperator())) {
				msInfo.setCommOperatorName(Tools.getDicNameByCodeAndValue(sessionFactory, "TXS", msInfo.getCommOperator()));
			}
			
			if (StringUtils.isNotBlank(msInfo.getMeterPattern())) {
				// 水表型式
				msInfo.setMeterPatternName(Tools.getDicNameByCodeAndValue(sessionFactory, "MMP", msInfo.getMeterPattern()));
			}
			
			if (StringUtils.isNotBlank(msInfo.getMeterSensor())) {
				// 水表传感器
				msInfo.setMeterSensorName(Tools.getDicNameByCodeAndValue(sessionFactory, "MSS", msInfo.getMeterSensor()));
			}
			
			if(StringUtils.isNotBlank(msInfo.getProtocol())) {
				//下行规约
				msInfo.setProtocolName(Tools.getDicNameByCodeAndValue(sessionFactory, "XXGY", msInfo.getProtocol()));
			}
			if(StringUtils.isNotBlank(msInfo.getValveStatus())) {
				//阀门状态
				msInfo.setValveStatusName(Tools.getDicNameByCodeAndValue(sessionFactory, "MVS", msInfo.getValveStatus()));
			}
			if(msInfo.getValveControl() != null) {
				if(1 == msInfo.getValveControl())
					msInfo.setValveControlName("是");
				if(0 == msInfo.getValveControl())
					msInfo.setValveControlName("否");
			}
			MeterBoreMapper mbMapper = sessionFactory.getMapper(MeterBoreMapper.class);
			if (!StringUtils.isBlank(msInfo.getMeterBore())){
				MeterBoreBean meterBoreBean = mbMapper.selectMeterBoreById(msInfo.getMeterBore());
				if (meterBoreBean != null){
					msInfo.setMeterBoreName(meterBoreBean.getBoreName());
				} else 
					msInfo.setMeterBoreName(msInfo.getMeterBore());
			}

			// 获取型号
			if (!StringUtils.isBlank(msInfo.getMeterModel())) {
				MeterModelMapper mmMapper = sessionFactory.getMapper(MeterModelMapper.class);
				MeterModelBean meterModelBean = mmMapper.selectMeterModelById(msInfo.getMeterModel());
				if (meterModelBean != null) {
					msInfo.setMeterModelName(meterModelBean.getModelName());
				}else
					msInfo.setMeterModelName(msInfo.getMeterModel());
			}
			// 获取水表厂家
			if (!StringUtils.isBlank(msInfo.getMeterFactory())) {
				MeterFactoryMapper mfMapper = sessionFactory.getMapper(MeterFactoryMapper.class);
				MeterFactoryBean meterFactoryBean = mfMapper.selectMeterFactoryById(msInfo.getMeterFactory());
				if (meterFactoryBean != null) {
					msInfo.setMeterFactoryName(meterFactoryBean.getFactoryName());
				}else
					msInfo.setMeterFactoryName(msInfo.getMeterFactory());
			}
			
		
			
		}
		return msInfo;
	}
	
}
